<template>
  <el-dialog
    v-model="dialogVisible"
    :title="isEdit ? '设置角色' : '创建组员'"
    width="500"
  >
    <el-form :model="MemberForm" ref="MemberFormRef" label-width="auto">
      <el-form-item
        label="组员"
        props="acc"
        :rules="[
          {
            required: true,
            message: '请选择组员',
            trigger: 'change',
          },
        ]"
      >
        <el-select
          v-model="MemberForm.acc"
          filterable
          remote
          reserve-keyword
          placeholder="按 姓名/学号 搜索"
          :remote-method="remoteMemberMethod"
          :loading="selectLoading"
          clearable
          :disabled="isEdit"
        >
          <el-option
            v-for="item in memberOptions"
            :key="item.id"
            :label="item.name"
            :value="item.acc"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="MemberForm.identity" placeholder="请选择角色">
          <el-option v-for="item in roleType" :label="item" :value="item" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="电话">
        <el-input v-model="MemberForm.phone" placeholder="请输入电话" />
      </el-form-item>-->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onAppend(MemberFormRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import api from "@/api";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { roleType } from "./utils";

const emit = defineEmits(["refresh"]);

const dialogVisible = ref<boolean>(false);

const MemberFormRef = ref();
const MemberForm = ref<any>({
  groupId: "",
  acc: "",
  identity: "",
  phone: "",
});

const isEdit = ref(false);

const memberOptions = ref<any[]>([]);
const selectLoading = ref(false);

watch(dialogVisible, (bool) => {
  if (!bool) {
    MemberForm.value = {
      groupId: "",
      acc: "",
      identity: "",
      phone: "",
    };
    isEdit.value = false;
  }
});
const remoteMemberMethod = (query: any) => {
  if (query) {
    selectLoading.value = true;
    setTimeout(() => {
      api.user
        .selectStudentByName(query)
        .then((res: any) => {
          memberOptions.value = res.success ? res.data : [];
          selectLoading.value = false;
        })
        .catch(() => {
          selectLoading.value = false;
        });
    }, 200);
  } else {
    memberOptions.value = [];
  }
};

function onAppend(formEl: FormInstance | undefined) {
  if (!formEl) return;
  //@ts-ignore
  formEl.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        api.groupDetail.updateGroup(MemberForm.value).then((res: any) => {
          ElMessage({
            message: res.message,
            type: res.success ? "success" : "error",
            grouping: true,
            showClose: true,
          });
          if (res.success) {
            emit("refresh");
            dialogVisible.value = false;
          }
        });
      } else {
        api.groupDetail.insertGroupDetail(MemberForm.value).then((res: any) => {
          ElMessage({
            message: res.message,
            type: res.success ? "success" : "error",
            grouping: true,
            showClose: true,
          });
          if (res.success) {
            emit("refresh");
            dialogVisible.value = false;
          }
        });
      }
    }
  });
}

const open = (groupId: any, row: any) => {
  MemberForm.value.groupId = groupId;
  if (row) {
    remoteMemberMethod(row.name);
    MemberForm.value = {
      id: row?.id,
      groupId: row?.groupId,
      acc: row?.acc,
      identity: row?.identity,
      phone: row?.phone,
    };
    isEdit.value = true;
  }

  dialogVisible.value = true;
};

defineExpose({ open });
</script>
